#include <math.h>
#include <stdio.h>

void main() {
  FILE * arq = NULL, * arq1 = NULL, * arq2 = NULL;
  int matricula, i, rs;
  char c = ' ';
  float nota, soma;
	
  if((arq = fopen("arq_703_3.txt","r")) != NULL) {
	if(((arq1 = fopen("arq_703a_3.txt","w")) != NULL) && ((arq2 = fopen("arq_703b_3.txt","w")) != NULL)) {
	  while(c != EOF) {			
		fscanf(arq,"%d", &matricula);
		c = fgetc(arq);			
		soma = 0;
		for (i = 0; i < 4; i++) {
		  rs = fscanf(arq,"%f", &nota);
		  c = fgetc(arq);	
		  if (rs == 1) {				
			soma = soma + nota;
		  }
		}
		if(rs == 1) { 
		  if(soma/4 >= 5) 
			fprintf(arq1, "%d %f\n", matricula, soma/4);				
		  else 
			fprintf(arq2, "%d %f\n", matricula, soma/4);
		}
	  }		
	  fclose(arq);
	  fclose(arq1);
	  fclose(arq2);			
	}				
  }
}
